﻿#region Copyright
//Copyright 2012 Phelon Aka. Jon H.
/*
    This file is part of BGBuddy - Ultimate PVP Suite.

    BGBuddy - Ultimate PVP Suite is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BGBuddy - Ultimate PVP Suite is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Styx.Plugins;
using Styx;
using Styx.CommonBot;
using Styx.Helpers;
using Styx.CommonBot.POI;
using Styx.WoWInternals.WoWObjects;
using Styx.WoWInternals;
using Styx.Pathing;

namespace UltimatePvPSuite
{
    public static class Demolisher
    {
        public static WoWUnit Target = null;
        public static bool TargetDemolishers()
        {
            if (UltimatePvPSuite.EnableTargetDemolishers)
            {
                if (StyxWoW.Me.CurrentTarget != null)
                {
                    if (UltimatePvPSuite.CurrentTargetType == "Demolishers" && StyxWoW.Me.CurrentTarget.IsAlive
                        && StyxWoW.Me.CurrentTarget.Distance <= UltimatePvPSuite.TargetHealersRange
                        && UltimatePvPSuite.CurrentTarget.Guid == StyxWoW.Me.CurrentTarget.Guid)
                    {
                        BotPoi.Current = new BotPoi(UltimatePvPSuite.CurrentTarget, PoiType.Kill);
                        Target.Target();
                        return true;
                    }
                }
                Target = EnemyDemolisher();
                if (Target != null)
                {
                    if (Target.Guid != UltimatePvPSuite.CurrentTarget.Guid || (UltimatePvPSuite.EnableTargetingLoSLogic && !StyxWoW.Me.CurrentTarget.InLineOfSight))
                    {
                        Logger.Write("Vehicle Spotted! Switching to " + ((WoWUnit)Target).SafeName() + "!");
                        BotPoi.Current = new BotPoi(Target, PoiType.Kill);
                        UltimatePvPSuite.CurrentTarget = Target;
                        Target.Target();
                        return true;
                    }
                }
            }
            return false;
        }

        private static WoWUnit EnemyDemolisher()
        {
            using (StyxWoW.Memory.AcquireFrame())
            {
                return (from Unit in ObjectManager.GetObjectsOfType<WoWUnit>(false)
                        orderby Unit.HealthPercent
                        where Unit.IsAlive
                        where Unit.Distance < UltimatePvPSuite.TargetDemolishersRange
                        where !Unit.IsFriendly
                        where !Unit.IsPet
                        where Unit.Name.Contains("Demolisher")
                        where Navigator.CanNavigateFully(StyxWoW.Me.Location, Unit.Location)
                        select Unit).FirstOrDefault();
            }
        }
    }
}
